<?php

namespace app\common\model\user;

use app\api\model\User;
use think\Model;


class Follow extends Model
{

    // 表名
    protected $name = 'user_follow';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [];

    //关注 1/ 取消 0
    public static function focus($r, $l, $status, $type)
    {
        $relevance = self::where(['r_user_id' => $r, 'l_user_id' => $l, 'genre' => $type])->find();

        if ($relevance) {
            $relevance->status = $status;
            $relevance->save();
        } else {
            $relevance = new self();
            $relevance->r_user_id = $r;
            $relevance->l_user_id = $l;
            $relevance->genre = (int)$type;
            $relevance->status = $status;
            $relevance->save();
        }
        return $relevance;

    }

    public static function list($params, $user_id)
    {
        extract($params);
        $where['status'] = 1;
        $where['genre'] = 1;
        switch ($type) {
            case 1:
                $where['l_user_id'] = $user_id;
                break;
            case 2:
                $where['r_user_id'] = $user_id;
                break;
            case 3:
                $where['l_user_id'] = $user_id;
                $where['genre'] = 2;
                break;
        }

        return self::where($where)->paginate($pagesize)->each(function ($row) use ($user_id, $type) {
            if ($user_id == $row->r_user_id) {
                $fans_wehre = ['id' => $row->l_user_id];
                $where_status = ['r_user_id' => $row->l_user_id, 'l_user_id' => $user_id];
                $row->user_id = $row->l_user_id;
            } else {
                $fans_wehre = ['id' => $row->r_user_id];
                $where_status = ['r_user_id' => $user_id, 'l_user_id' => $row->r_user_id];
                $row->user_id = $row->r_user_id;
            }

            $status = self::where($where_status)->where('genre', 1)->value('status', 0);

            if ($status == 1) {
                $row->status = 2;
            } else {
                $row->status = $status;
            }

            if ($type == 2 && $row->status != 2) {
                $row->status = 1;
            }
            $row->fans_name = User::where($fans_wehre)->value('nickname', "");
            $row->fans_avatar = User::where($fans_wehre)->value('avatar', "");
        });
    }

    public static function nums($user_id)
    {
        $fans = self::where('l_user_id', $user_id)->where(['status' => 1, 'genre' => 1])->count();
        $keep = self::where('r_user_id', $user_id)->where(['status' => 1, 'genre' => 1])->count();
        $visit = self::where('l_user_id', $user_id)->where(['status' => 1, 'genre' => 2])->count();
        return ['fans' => $fans, 'keep' => $keep, 'visit' => $visit];
    }

    public function r_user()
    {
        return $this->belongsTo('app\common\model\User', 'r_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function l_user()
    {
        return $this->belongsTo('app\common\model\User', 'l_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
